package shopping.web.servlet;

import org.apache.commons.beanutils.BeanUtils;
import shopping.domain.User;
import shopping.service.UserService;
import shopping.utils.UUIDUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;

@WebServlet(urlPatterns = "/user")
public class UserServlet extends BaseServlet {
    /**
     * 注销功能方法
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    protected void logout(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //销毁session
        request.getSession().invalidate();
        //必须回写给客户端，这样客户端才能触发回调函数。
        success();
    }

    /**
     * 获取当前登录人的用户名
     *
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    protected void currentname(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取当前session是否存在一个user对象
        User user = (User) request.getSession().getAttribute("user");
        if(user != null){
            //登录状态
            String name = user.getName();
            success(name);
        }else{
            //没有登录，不用管
        }
    }

    protected void login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        /*
            --------登录功能--------
         */
        //获取用户名和密码
        String username = request.getParameter("username");
        String password = request.getParameter("password");

        //调用Service查询用户是否存在
        User user = new UserService().findUserByUAP(username, password);
        if (user != null) {
            //用户存在，可以进行登录
            //使用Session记录用户状态，
            request.getSession().setAttribute("user", user);
            success();
        } else {
            //登录失败
            fail("用户名或密码错误");
        }
    }

    protected void register(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        /*
            --------用户名长度校验--------
         */
        String username = request.getParameter("username");
        //约定用户名长度在2-6之间
        if (username == null || username.trim().length() < 2 || username.trim().length() > 6) {
            fail("用户名长度应该在2-6之间");
            return;
        }
        /*
            -------数据库存储代码--------
         */
        //1. 接受客户端数据
        Map<String, String[]> map = request.getParameterMap();
        //转换成JavaBean
        User user = new User();
        try {
            BeanUtils.populate(user, map);
        } catch (Exception e) {
        }
        //使用UUID工具类生成uid
        user.setUid(UUIDUtil.getId());
        //2. 调用Service、Dao方法将数据保存到数据库
        new UserService().save(user);
        //3. 返回成功标识（json格式数据）
        success();
    }
}
